<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

/**
 * App\Weibo
 *
 * @property int $id
 * @property int $wb_id
 * @property string $body
 * @property string $images
 * @property string $type
 * @property bool $is_shown
 * @property string $weibor
 * @property string $avatar
 * @property string $media_url
 * @property string $long_url
 * @property string $media_type
 * @property \Carbon\Carbon $created_at
 * @property \Carbon\Carbon $updated_at
 * @property-read mixed $image_urls
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereAvatar($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereBody($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereCreatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereImages($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereIsShown($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereLongUrl($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereMediaType($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereMediaUrl($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereType($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereUpdatedAt($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereWbId($value)
 * @method static \Illuminate\Database\Query\Builder|\App\Weibo whereWeibor($value)
 * @mixin \Eloquent
 */
class Weibo extends Model
{
    protected $fillable = ['raw_json','type','wb_id','body','images','avatar','weibor','media_url','long_url','media_type','is_shown'];


    public function setAvatarAttribute($value) {
        $value = stripslashes ($value);

        $this->attributes['avatar'] = preg_replace('#https*:#','https:',$value);
    }

    public function setImagesAttribute($value) {
        $value = stripslashes ($value);

        $value = str_ireplace('thumbnail_pic','pic',$value);
        $value = str_ireplace('thumbnail','large',$value);
        $this->attributes['images'] = preg_replace('#https*:#','https:',$value);
    }


    public function setBodyAttribute($value)
    {
        $value =stripslashes ($value);
        $pattern = '#(?i)\b((?:[a-z][\w-]+:(?:/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>\x7f-\xff]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>\x7f-\xff]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?«»“”‘’])[^\x7f-\xff])#';
        $ok = preg_match($pattern,$value,$matches);
        if($ok) {
            $this->attributes['media_url'] = $matches[0];
            $this->attributes['body'] = preg_replace($pattern,'',$value);
        } else {
            $this->attributes['body'] = $value;
        }
        if(str_contains($value,'删除')) {
            $this->attributes['is_shown'] = 3;
        }
    }

    public function getImageUrlsAttribute() {
        return json_decode($this->images);
    }

    public function setLongUrlAttribute($value)
    {
        $this->attributes['long_url'] = stripslashes($value);

        if(str_contains($value,'miaopai')) {
            $this->attributes['media_type'] = 'miaopai';
        }
        if(str_contains($value,'youku')) {
            $this->attributes['media_type'] = 'youku';
        }
        if(str_contains($value,'bilibili')) {
            $this->attributes['media_type'] = 'bili';
        }
        if(str_contains($value,'acfun')) {
            $this->attributes['media_type'] = 'acfun';
        }
        if(str_contains($value,'weibo')) {
            $this->attributes['media_type'] = 'weibo';
        }
        if(str_contains($value,'xiaokaxiu')) {
            $this->attributes['media_type'] = 'xiaokaxiu';
        }
        if(str_contains($value,'vlook')) {
            $this->attributes['media_type'] = 'vlook';
        }

    }

}
